Practical Bayesian Inference by CORYN A. L. BAILER-JONES

Practical Bayesian Inference by CORYN A. L. BAILER-JONES

Author:CORYN A. L. BAILER-JONES
Language: eng
Format: epub
Publisher: Cambridge University Press


library(MASS) # for truehist and geyser data

attach(geyser)

pdf("kde.pdf", 8,4)

par(mfrow=c(1,2), mar=c(3.5,3.0,0.5,0.5), oma=c(0.5,0.5,0.5,0.5),

mgp=c(2.2,0.8,0), cex=1.0)

truehist(duration, h=0.25, xlim=c(0.5, 6), ymax=1.1, ylab="density",

col="white", lwd=0.5)

lines(density(duration, kernel="rectangular", bw=0.1, n=2^10), lwd=1.5)

lines(density(duration, kernel="rectangular", bw=0.2, n=2^10), lwd=2,

col="grey60")

truehist(duration, h=0.25, xlim=c(0.5, 6), ymax=1.1, ylab="density",

col="white", lwd=0.5)

lines(density(duration, kernel="gaussian", bw=0.1, n=2^10), lwd=1.5)

lines(density(duration, kernel="gaussian", bw=0.2, n=2^10), lwd=2,

col="grey60")

dev.off()

detach(geyser)

You may be interested in looking at what the function density actually produces. If you do

attach(geyser)

mydense <- density(duration, kernel="gaussian", bw=0.2, n=2^10)

then attributes(mydense) lists all the things computed and stored by density. This produces the following.

$names

[1] "x" "y" "bw" "n" "call" "data.name" "has.na"

$class

[1] "density"

The attribute x is the vector of n points at which the density was estimated, to give the values y. You can retrieve these values with mydense$x. Note that density estimation does not produce an analytic function, but rather evaluations of the estimator at a pre-defined set of points. The function lines in the above code just connects the x and y values in order to plot an apparently continuous function.

The idea of kernel density estimation extends to higher dimensions. The following code is an example in two dimensions applied to the geyser data. It produces figure 7.4.

R file: kde2d.R

##### Investigate kernel density estimation in 2D

##### Modified from the example given by Venables & Ripley (2002)



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.